package cn.liziguo.tank.collision;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.BulletType;
import cn.liziguo.tank.constants.CampEnum;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.PropertyConst;
import cn.liziguo.tank.factory.EffectsFactory;
import cn.liziguo.tank.script.BulletComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;

/**
 * @author Liziguo
 * @date 2022-07-09 21:26:11
 */
public class BulletBulletCollisionHandler extends CollisionHandler {

    public BulletBulletCollisionHandler() {
        super(GameEnum.BULLET, GameEnum.BULLET);
    }

    @Override
    protected void onCollisionBegin(Entity a, Entity b) {
        // 只有不同阵营的子弹才会碰撞
        if (a.getObject(PropertyConst.CAMP) != b.getObject(PropertyConst.CAMP)) {
            Object aType = a.getObject(PropertyConst.BULLET_TYPE);
            Object bType = b.getObject(PropertyConst.BULLET_TYPE);
            if (aType == BulletType.PLUS && bType == BulletType.PLUS) {
                // 两发超级子弹碰撞 立即爆炸
                a.removeFromWorld();
                b.removeFromWorld();
                EffectsFactory.createBigBoom(a.getCenter(), GameConfig.BULLET_PLUS_BOOM_SIZE, a.getObject(PropertyConst.CAMP) == CampEnum.PLAYER, GameConfig.BULLET_PLUS_BOOM_ATTACK);
                EffectsFactory.createBigBoom(b.getCenter(), GameConfig.BULLET_PLUS_BOOM_SIZE, b.getObject(PropertyConst.CAMP) == CampEnum.PLAYER, GameConfig.BULLET_PLUS_BOOM_ATTACK);
            } else if (aType == BulletType.PLUS && bType == BulletType.NORMAL) {
                // 超级子弹撞普通子弹
                b.removeFromWorld();
            } else if (aType == BulletType.NORMAL && bType == BulletType.NORMAL) {
                // 普通子弹撞普通子弹
                int attackA = a.getComponent(BulletComponent.class).attack;
                int attackB = b.getComponent(BulletComponent.class).attack;
                // 比较伤害 移除伤害低的 如果相同则两个都移除
                if (attackA == attackB) {
                    a.removeFromWorld();
                    b.removeFromWorld();
                } else if (attackA > attackB) {
                    b.removeFromWorld();
                } else {
                    a.removeFromWorld();
                }
                EffectsFactory.createBoom(a.getCenter().add(b.getCenter()).multiply(.5));
            }

        }
    }
}
